package cn.dujc.superkid.activities;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.dujc.core.ui.BaseActivity;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.configs.Globals;

public class ForgetPasswordActivity extends BaseActivity implements View.OnClickListener {
    private final String TAG = ForgetPasswordActivity.class.getName();
    private Button resetPwd;
    private TextView et_phone;
    private TextView code_text;
    private TextView register_send_code;
    private TextView et_password;
    private String codekey;
    private CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long l) {
            int ms = (int) (l / 1000);
            register_send_code.setText(ms + "s后重新获取");
            register_send_code.setClickable(false);
        }

        @Override
        public void onFinish() {
            register_send_code.setText("重新发送");
            register_send_code.setClickable(true);
        }
    };

    @Override
    public int getViewId() {
        return R.layout.activity_forget_password;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
        setTitle("忘记密码");
    }
    private void initView(){
        resetPwd = findViewById(R.id.btn_resetPwd);
        et_password = findViewById(R.id.et_password);
        code_text = findViewById(R.id.code_text);
        register_send_code=findViewById(R.id.register_send_code);
        et_phone = findViewById(R.id.et_phone);
        register_send_code.setOnClickListener(this);
        resetPwd.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        final String phone = et_phone.getText().toString();
        final String password = et_password.getText().toString();
        final String codetext = code_text.getText().toString();
        switch (v.getId()){
            case R.id.register_send_code:
                if (!phoneCheck(Globals.PHONE_CHECK,phone)){
                    ToastUtil.showToast(mActivity, "请输入正确的手机号");
                }
                else {

                    HttpHelper.getLoginRegister().getCode(QMap.build()
                            .put("username", phone).put("is_exist", "2")
                            .create()).enqueue(new ResultCallBack<Result<String>>(mActivity) {
                        @Override
                        public void onSuccessCallback(int code, Result<String> body) {
                            if (body != null) {
                                ToastUtil.showToast(mActivity, body.getMsg());
                                codekey=body.getData();
                                startCount();
                            }
                        }
                    });
                }
                break;
            case R.id.btn_resetPwd:
                if (!phoneCheck(Globals.PHONE_CHECK,phone)){
                    ToastUtil.showToast(mActivity, "请输入正确的手机号");
                }
                else if (code_text==null) {
                    ToastUtil.showToast(mActivity, "验证码不能为空");
                }
                else {
                    Log.i(TAG,"==============caizhijia======="+codekey );
                    Log.i(TAG,"==============caizhijia======="+codetext );
                    Log.i(TAG,"==============caizhijia======="+phone );
                    Log.i(TAG,"==============caizhijia======="+password );
                    HttpHelper.getLoginRegister().userGetPassword(QMap.build()
                            .put("username", phone)
                            .put("password", password)
                            .put("code", codetext)
                            .put("codekey", codekey)
                            .create()).enqueue(new ResultCallBack<Result<String>>(mActivity) {
                        @Override
                        public void onSuccessCallback(int code, Result<String> body) {
                            if (body != null) {
                                ToastUtil.showToast(mActivity, body.getMsg());
                                Log.i(TAG,"==============caizhijia======="+body.getMsg() );
                                finish();
                            }
                        }
                    });
                }
                break;
            default:
                break;
        }
    }
    private void startCount() {
        countDownTimer.start();
    }
    public static boolean phoneCheck(String patttern, String username){
        Pattern pattern = Pattern.compile(patttern);
        Matcher matcher = pattern.matcher(username);
        if(matcher.find()){
            return  true;
        }
        else {
            return false;
        }
    }
}
